package com.appspot.bs24event.controller.event;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.datanucleus.util.StringUtils;
import org.slim3.controller.BaseController;
import org.slim3.controller.Navigation;
import org.slim3.util.BeanUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.appspot.bs24event.meta.EventMeta;
import com.appspot.bs24event.model.Event;
import com.appspot.bs24event.service.EventService;

/**
 * DetailController.
 * @version $Id: DetailController.java 155 2011-07-26 03:31:58Z bs24event@gmail.com $
 */
public class DetailController extends BaseController {

    /** ログ */
    private static final Logger logger = Logger.getLogger(DetailController.class.getName());

    /** リソースバンドル */
    private static ResourceBundle settingProperties = ResourceBundle.getBundle("setting");

    private EventService service = new EventService();

    private EventMeta meta = EventMeta.get();

    @Override
    protected Navigation setUp() {
        logger.info(this.getClass().getName() + " start.");
        return super.setUp();
    }

    @Override
    protected void tearDown() {
        logger.info(this.getClass().getName() + " end.");
        super.tearDown();
    }

    @Override
    public Navigation run() throws Exception {
        // イベント一覧の取得
        Event event = service.get(asKey(meta.key), asLong(meta.version));
        // 備考について、改行だけできるようサニタイズ
        if ( ! StringUtils.isEmpty(event.getNote()) ) {
            event.setNote(
                event.getNote()
                .replaceAll("&","&amp;")
                .replaceAll("<","&lt;")
                .replaceAll(">","&gt;")
                .replaceAll("'","&#39;")
                .replaceAll("\"","&quot;")
                .replaceAll("  ","&t;")
                .replaceAll("\r\n", "<br />")
                .replaceAll("\n", "<br />")
                .replaceAll("\r", "<br />")
            );
        }
        BeanUtil.copy(event, request);
        requestScope("event", event);

        // イベント修正画面へ遷移
        return forward(request, "detail.jsp", true, true);
    }
}
